package admin.controllers;

import helpers.IdUniqueHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import entities.Application;
import entities.Category;

import admin.forms.CategoryForm;
import admin.models.ApplicationModel;
import admin.models.CategoryModel;

public class CategoryEditServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		String path = ((HttpServletRequest) request).getRequestURI();
		String params = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		if (params == null || params.isEmpty()) {
			response.sendRedirect("/admin/appmapping/list");
			return ;
		}
		
		String[] pr = params.split("/");
		
		if (pr.length < 2) {
			response.sendRedirect("/admin/appmapping/list");
			return ;
		}
		
		String id = pr[0];
		String appId = pr[1];
		
		ApplicationModel appModel = ApplicationModel.getInstance();
		Application app = appModel.get(appId);
		
		if (app == null) {
			response.sendRedirect("/admin/application/list");
			return ;
		}
		
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		CategoryModel categoryModel = CategoryModel.getInstance();
		Category category = categoryModel.getCategory(appId, id);
		
		request.setAttribute("form", "edit");
		request.setAttribute("check", check);
		request.setAttribute("category", category);
		request.setAttribute("app", app);
		try {
			request.getRequestDispatcher("/admin/pages/category_form.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		String path = ((HttpServletRequest) request).getRequestURI();
		String params = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		if (params == null || params.isEmpty()) {
			response.sendRedirect("/admin/appmapping/list");
			return ;
		}
		
		String[] pr = params.split("/");
		
		if (pr.length < 2) {
			response.sendRedirect("/admin/appmapping/list");
			return ;
		}
		
		String id = pr[0];
		String appId = pr[1];
		
		ApplicationModel appModel = ApplicationModel.getInstance();
		Application app = appModel.get(appId);
		
		if (app == null) {
			response.sendRedirect("/admin/application/list");
			return ;
		}
		request.setAttribute("appId", appId);
		
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		CategoryModel categoryModel = CategoryModel.getInstance();
		Category category = categoryModel.getCategory(appId, id);
		
		TreeMap<String, Object> form = (TreeMap<String, Object>) CategoryForm.checkValid(request, category);
		category = (Category) form.get("category");
		check = (TreeMap<String, String>) form.get("checkValid");
		
		if (check.size() == 0) {
			categoryModel.insert(appId, category);
			category = new Category();
			category.setId(IdUniqueHelper.getId());
			
			request.setAttribute("success", "Add success category " + category.getName());
		}
		
		request.setAttribute("form", "edit");
		request.setAttribute("check", check);
		request.setAttribute("category", category);
		request.setAttribute("app", app);
		try {
			request.getRequestDispatcher("/admin/pages/category_form.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
